package com.example.library.controller;

import com.example.library.constant.Constants;
import com.example.library.enums.ResultCode;
import com.example.library.model.*;
import com.example.library.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-16
 * Time: 23:22
 */
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired//告诉spring要从容器中去除这个对象, 赋值给当前对象.
    private BookService bookService;
    @RequestMapping("/getBookListByPage")
    public Result getBookListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("查询翻页信息, pageRequest={}", pageRequest);

//        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if (userInfo == null || userInfo.getId()<0||"".equals(userInfo.getUserName())) {
//            return Result.unlogin();
//        }
         //校验登录成功
        if (pageRequest.getPageSize() < 0 || pageRequest.getCurrentPage() < 1) {
            return Result.fail("参数校验失败");
        }
        PageResult<BookInfo> bookInfoPageResult = null;
        try {
            bookInfoPageResult = bookService.selectBookInfoByPage(pageRequest);
            return Result.success(bookInfoPageResult);
        } catch (Exception e) {
            log.error("查询翻页信息错误, e{}", e.getMessage());
            return Result.fail(e.getMessage());
        }

    }

    @RequestMapping(value = "/addBook", produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        if (bookInfo == null ||
                !StringUtils.hasLength(bookInfo.getBookName()) ||
                !StringUtils.hasLength(bookInfo.getAuthor()) ||
                !StringUtils.hasLength(bookInfo.getPublish())||
                bookInfo.getCount() < 0 ||
                bookInfo.getPrice() == null) {

            return "参数校验失败, 请检查输入的参数";
        }
        Integer result = bookService.addBook(bookInfo);
        if (result <= 0) {
            log.error("添加图书出错, book:{}", bookInfo.toString());
            return "添加图书出错, 请联系管理员";
        }
        return "";
    }
    @RequestMapping("/queryBookInfoById")
    public BookInfo queryBookInfoById(Integer bookId) {

        return bookService.queryBookInfoById(bookId);
    }
    @RequestMapping(value = "/updateBook", produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        Integer ret = bookService.updateBook(bookInfo);
        if (ret == 0) {
            log.error("更新图书失败, 请联系管理员");
            return "更新失败";
        }
        return "";
    }
    @RequestMapping(value = "/batchDelete", produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids) {
        Integer ret = bookService.batchDelete(ids);
        if (ret == 0) {
            return "删除失败";
        }
        return "";
    }
}
